////////////////////////////////////////////////////////////////////////////////
//////// ELECTION DATES DATABASE 
////////////////////////////////////////////////////////////////////////////////

// Paths of input files
local dates_ac = "$project_path/data/1_input/elections/Election dates/dates_ac.dta"
local dates_ifes = "$project_path/data/1_input/elections/Election dates/dates_ifes.dta"
local dates_wikipedia = "$project_path/data/1_input/elections/Election dates/dates_wikipedia.dta"
local dates_wikipedia_infobox = "$project_path/data/1_input/elections/Election dates/dates_wikipedia_infobox.dta"
local dates_wikidata = "$project_path/data/1_input/elections/Election dates/dates_wikidata.dta"
local dates_vdem = "$project_path/data/2_intermediary/elections/Election dates/dates_vdem.dta"
local dates_parlgov = "$project_path/data/2_intermediary/elections/Election dates/dates_parlgov.dta"
local dates_mp = "$project_path/data/2_intermediary/elections/Election dates/dates_mp.dta"

// Priority list 
local priority_list = "vdem parlgov mp wikipedia ac ifes wikipedia_infobox wikidata"

// We merge input files
use "`dates_vdem'", clear
ren Date Date_vdem
foreach source in parlgov mp wikipedia ac ifes wikipedia_infobox wikidata {
	merge 1:1 Country Year Month Type_Election using "`dates_`source''", nogen
	ren Date Date_`source'
}

// We use the highest source in the priority list to populate the Date variable
gen Date = .
format Date %td
foreach source in `priority_list' {
	replace Date = Date_`source' if Date==.
}
drop Date_*

// Saving results
sort Type_Election Country Year Month Date
order Country Year Month Type_Election Date
format Country %20s
lab var Country  "Country in which election took place"
lab var Year     "Year of the election"
lab var Month    "Month of the election"
lab var Type_Election    "Election type"
lab var Date "Election date"
notes drop _dta
compress

save "$project_path/data/2_intermediary/elections/Election dates/election_dates.dta", replace

